package com.interview.thread.philosproblem;

import com.interview.thread.SleepHelper;

public class DeadLockTest {
    public static void main(String[] args) {
        Chopsticks c0 = new Chopsticks();
        Chopsticks c1 = new Chopsticks();
        Chopsticks c2 = new Chopsticks();
        Chopsticks c3 = new Chopsticks();
        Chopsticks c4 = new Chopsticks();

        Philosopher philosopher0 = new Philosopher("0",0,c0,c1);
        Philosopher philosopher1 = new Philosopher("1",1,c1,c2);
        Philosopher philosopher2 = new Philosopher("2",2,c2,c3);
        Philosopher philosopher3 = new Philosopher("3",3,c3,c4);
        Philosopher philosopher4 = new Philosopher("4",4,c4,c0);
        philosopher0.start();
        philosopher1.start();
        philosopher2.start();
        philosopher3.start();
        philosopher4.start();

    }

}


class Philosopher extends Thread {
    //哲学家名称
    private String name;
    //哲学家位置
    private Integer index;

    Chopsticks left ,right;

    Philosopher(String name,Integer index,Chopsticks left,Chopsticks right){
        this.name =name;
        this.index = index;
        this.left = left;
        this.right = right;
    }




    @Override
    public void run() {
        synchronized (left){
            SleepHelper.sleepSeconds(1);
            synchronized (right){
                SleepHelper.sleepSeconds(1);
                System.out.println(name+"吃完了");
            }
        }
    }


}
